<?php
/** 
 * Car_model
 * This class handles model work necessary for logging in user
 * 
 * @author Ray Nowell
 *	
 */ 
class Car_model extends CI_Model {

	function Car_model()
	{
		parent::__construct();
	}
	
	function Delete_car_image($mcg_id)
	{
        $this->db->delete('member_car_gallery', array('mcg_id' => $mcg_id)); 
	}
    
	function Get_car_image($mcg_id) {
		$this->db->from('member_car_gallery');
		$this->db->where('mcg_id', $mcg_id);
		$query = $this->db->get();
		
		return $query->row();
	}
    
	function Load_car_info($car_id)
	{
		$select_fields =	"	 car_id
								,nickname
								,year
								,model_id
								,engine_id
								,DATE_FORMAT(owned_from,'%c/%e/%Y') AS owned_from
								,DATE_FORMAT(owned_to,'%c/%e/%Y') AS owned_to
								,for_sale
								,CONCAT('$', FORMAT(price, 2)) AS price
								,notes
								,model.short_name as model
								,engine.short_name as engine
								,CASE for_sale WHEN 1 THEN 'Yes' Else 'No' END as for_sale_text
								,member.member_id
                                ,CASE contact.short_name
                                    WHEN 'Phone' THEN member.phone
                                    WHEN 'Email' THEN member.email
                                    WHEN 'Facebook' THEN member.facebook_username
                                    WHEN 'Twitter' THEN member.twitter_username
                                    ELSE member.email
                                END AS contact
							";
		$this->db->select($select_fields, false);
		$this->db->from('member');
        $this->db->join('member_car', 'member.member_id = member_car.member_id');
		$this->db->join('lookup AS model', 'member_car.model_id = model.lookup_id', 'left'); 
		$this->db->join('lookup AS engine', 'member_car.engine_id = engine.lookup_id', 'left'); 
		$this->db->join('lookup AS contact', 'member.contact_method_id = contact.lookup_id', 'left');
		$this->db->where('car_id', $car_id);
		$query = $this->db->get();
		
		return $query->row();
	}
    
	function Get_car_gallery($car_id)
	{
		$member_id = $this->session->userdata('member_id');
		$select_fields =	"	 mcg_id
								,car_id
								,pic_name
								,is_display_photo
								";
		$this->db->select($select_fields, false);
		$this->db->from('member_car_gallery');
		$this->db->where('car_id', $car_id);
        $query = $this->db->get();
		 
		return $query->result_array();
	}

	function Update_car($data)
	{
		$this->db->where('car_id',$data['car_id']);
		return $this->db->update('member_car',$data);
	}
    
	function Add_car($data)
	{
		$this->db->insert('member_car', $data);
		return $this->db->insert_id();
	}
    
	function Add_car_image($data)
	{
        $query = $this->db->query('SELECT * FROM member_car_gallery WHERE car_id = '.$data['car_id']);
        if ($query->num_rows() == 0)
            $data['is_display_photo'] = 1;
        
        
		$this->db->insert('member_car_gallery', $data);
		return;
	}
    
	function Make_profile_photo($profile_photo_id)
	{
        $car_id = '';
        $car_id_query = $this->db->query('SELECT car_id FROM `member_car_gallery` WHERE mcg_id = '.$profile_photo_id);
        
        foreach ($car_id_query->result() as $row) {
            $car_id = $row->car_id;
        }
        
        $this->db->simple_query('UPDATE `member_car_gallery` SET is_display_photo = 0 WHERE car_id ='.$car_id);
        
        $data = array('is_display_photo' => 1);
        $this->db->where('mcg_id', $profile_photo_id);
        $this->db->update('member_car_gallery', $data); 
        
        return $car_id;
	}
	
}

/* End of file car_model.php */
/* Location: ./system/application/models/car_model.php */